<template>
  <d2-container>
    <div class="mod-config">
      <div class="search">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(true)" size="small">
          <el-form-item style="margin-top:0 !important;margin-bottom:0 !important;">
            <el-input v-model="dataForm.key" placeholder="项目名称" clearable></el-input>
          </el-form-item>
          <el-form-item style="margin-top:0 !important;margin-bottom:0 !important;">
            <el-button @click="getDataList(true)">查询</el-button>
            <el-button v-if="isAuth('ecs:ecsprojectinfo:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
            <el-button v-if="isAuth('ecs:ecsprojectinfo:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%; margin-top:15px;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <!-- <el-table-column
          prop="projectId"
          header-align="center"
          align="center"
          label="项目编号">
        </el-table-column> -->
        <el-table-column
          prop="projectCode"
          header-align="center"
          align="center"
          label="项目编码">
        </el-table-column>
        <el-table-column
          prop="projectName"
          header-align="center"
          align="center"
          label="项目名称">
        </el-table-column>
        <el-table-column
          prop="projectHead"
          header-align="center"
          align="center"
          label="项目领导">
        </el-table-column>
        <el-table-column
          prop="projectTel"
          header-align="center"
          align="center"
          label="项目联系电话">
        </el-table-column>
        <el-table-column
          prop="co2Target"
          header-align="center"
          align="center"
          label="碳减排目标(tCO2e)">
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          label="创建时间">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.projectId)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.projectId)">删除</el-button>
            <el-button type="text" size="small" @click="changePro(scope.row)">切换到该项目</el-button>
            <el-button type="text" size="small" @click="reportPrint(scope.row)">诊断报告书</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="mt-4"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>

    <report-print id="project" ref="reportPrint" />
  </d2-container>
</template>

<script>
  import * as Util from "@/utils";
  import AddOrUpdate from './ecsprojectinfo-add-or-update'
  import reportPrint from '../components/reportPrint/index'
  import jp from '@/utils/jumpPagevalue.js';
  export default {
    name: 'ecs_ecsprojectinfo',
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,
      reportPrint
    },
    computed:{
      containerHeight() {
        return Util.getClientHeight() - 300;
      },
    },
    activated () {
      this.getDataList()
    },
    methods: {
      reportPrint(row) {
        this.$nextTick(() => {
          this.$refs.reportPrint.print([row])
        })
      },
      changePro(value) {
        jp.$emit('projectChange',value)
      },
      // 获取数据列表
      getDataList (type) {
        if(type){
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$api.ecs_ecsprojectinfo_list({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'paramKey': this.dataForm.key,
          "customerId": this.$util.cookies.get('customer_id'),
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.projectId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.ecs_ecsprojectinfo_delete(ids).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
